package com.sto.stoapp.activity.scanner.inquiry;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.sto.stoapp.activity.R;
import com.sto.stoapp.adapter.ShowCastAdapter;
import com.sto.stoapp.broadcast.HomeBroadAction;
import com.sto.stoapp.db.manager.CastManager;
import com.sto.stoapp.entity.DownCastEntity;
import com.sto.stoapp.widget.StoHomeActivity;

import java.util.ArrayList;
import java.util.List;

import sto.android.app.StoPdaKeyEvent;

public class ShowCastRecordActivity extends StoHomeActivity {

    private ListView recordListView;
    private ShowCastAdapter adapter;
    private List<DownCastEntity> entityLists = new ArrayList<DownCastEntity>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_cast_record_list);
        setTitleBottom(R.string.cast_record, HomeBroadAction.PICKER_BROAD);
        initWidget();
        initListen();
        Intent intent = getIntent();
        String date = intent.getStringExtra("date");
        String flight = intent.getStringExtra("flight");
        QueryThread thread = new QueryThread(this, date, flight);
        thread.start();
    }

    private void initWidget() {
        recordListView = (ListView) findViewById(R.id.cast_record_list);
        recordListView.setFocusable(true);
        adapter = new ShowCastAdapter(ShowCastRecordActivity.this, entityLists);
        recordListView.setAdapter(adapter);
    }

    private void initListen() {
        recordListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                arg1.setSelected(true);
                DownCastEntity entity = entityLists.get(arg2);
                showRecordDetail(getTextMenu(entity));
            }

        });
    }

    private String[] getTextMenu(DownCastEntity entity) {
        String[] textMenu = new String[5];
        textMenu[0] = "包号:" + entity.getBarCode();
        textMenu[1] = "航班:" + entity.getFlight();
        textMenu[2] = "路由:" + entity.getRouteCode();
        textMenu[3] = "日期:" + entity.getScanDate();
        textMenu[4] = "网点:" + entity.getSiteName();
        return textMenu;
    }

    /**
     * 显示记录详情
     */
    private void showRecordDetail(String[] textMenu) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("记录详情").setIcon(android.R.drawable.ic_dialog_info)
                .setNegativeButton("取消", null).setItems(textMenu, null);
        builder.setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(DialogInterface dialog, int keyCode,
                    KeyEvent event) {
                if (keyCode == StoPdaKeyEvent.KEYCODE_DEL
                        && event.getAction() == KeyEvent.ACTION_UP) {
                    dialog.dismiss();
                }
                return false;
            }
        });
        builder.show();
    }

    /**
     * 查询数据
     * @author mgf
     *
     */
    class QueryThread extends Thread {

        private Context mContext;
        private String mDate;
        private String mFlight;

        public QueryThread(Context context, String date, String flight) {
            mContext = context;
            mDate = date;
            mFlight = flight;
        }

        @Override
        public void run() {
            List<DownCastEntity> lists = CastManager.getInstance(mContext)
                    .queryRecordByDate(mDate, mFlight);
            for (DownCastEntity downCastEntity : lists) {
                entityLists.add(downCastEntity);
            }
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    adapter.notifyDataSetChanged();
                }
            });
        }
    }


}
